package cn.tedu.system.oa.dept.service.impl;

import cn.tedu.system.oa.base.exception.ServiceException;
import cn.tedu.system.oa.base.response.StatusCode;
import cn.tedu.system.oa.dept.mapper.DeptMapper;
import cn.tedu.system.oa.dept.pojo.dto.DeptQuery;
import cn.tedu.system.oa.dept.pojo.entity.Dept;
import cn.tedu.system.oa.dept.pojo.param.DeptSaveParam;
import cn.tedu.system.oa.dept.service.DeptService;
import cn.tedu.system.oa.user.mapper.UserMapper;
import cn.tedu.system.oa.user.pojo.dto.UserQuery;
import cn.tedu.system.oa.user.pojo.entity.User;
import cn.tedu.system.oa.user.pojo.vo.UserVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DeptServiceImpl implements DeptService {
    @Autowired
    DeptMapper deptMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Dept> selectDept(DeptQuery deptQuery) {
        log.debug("查询部门业务:参数={}",deptQuery);
        QueryWrapper<Dept> queryWrapper = new QueryWrapper();
        if (deptQuery != null){
            if(deptQuery.getDeptName() != null && deptQuery.getDeptName().trim().length() != 0){
                queryWrapper.like("dept_name",deptQuery.getDeptName());
            }else if(deptQuery.getStatus() != null){
                queryWrapper.eq("status",deptQuery.getStatus());
            }else if(deptQuery.getUserId() != null){
                queryWrapper.eq("user_id",deptQuery.getUserId());
            }else if(deptQuery.getLeader() != null && deptQuery.getLeader().trim().length() != 0){
                queryWrapper.like("leader",deptQuery.getLeader());
            }else if(deptQuery.getId() != null){
                queryWrapper.eq("id",deptQuery.getId());
            }
        }
        List<Dept> list = deptMapper.selectList(queryWrapper);
        if(list != null && list.size() > 0){
            return list;
        }
        return null;
    }

    @Override
    public void saveDept(DeptSaveParam deptSaveParam) {
        log.debug("保存部门数据业务,参数={}",deptSaveParam);
        //根据负责人名称获取负责人id
        String username = getLeaderByUserId(deptSaveParam.getUserId());
        Dept dept = new Dept();
        BeanUtils.copyProperties(deptSaveParam,dept);
        if(dept.getId() == null){
            dept.setLeader(username);
            dept.setCreateTime(new Date());
            deptMapper.insert(dept);
            //修改用户部门id
            updateUserDept(deptSaveParam.getUserId(),null,dept.getDeptName());
        }else {
            dept.setUpdateTime(new Date());
            dept.setLeader(username);
            UpdateWrapper updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",dept.getId());
            deptMapper.update(dept,updateWrapper);
            updateUserDept(deptSaveParam.getUserId(),dept.getId(),dept.getDeptName());
        }
    }

    private void updateUserDept(Integer userId,Integer deptId, String deptName) {
        if(deptId == null){
            QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dept_name",deptName);
            Dept dept = deptMapper.selectOne(queryWrapper);
            User user = new User();
            user.setId(userId);
            user.setDeptId(dept.getId());
            userMapper.update(user);
        }else {
            User user = new User();
            user.setId(userId);
            user.setDeptId(deptId);
            userMapper.update(user);
        }
    }

    //根据负责人名称获取负责人id
    private String getLeaderByUserId(Integer userId) {
        UserQuery query = new UserQuery();
        query.setId(userId);
        List<UserVO> list = userMapper.selectUser(query);
        return list.get(0).getUsername();
    }

    @Override
    public void deleteDept(Integer deptId) {
        log.debug("删除部门业务,参数={}");
        UserQuery query = new UserQuery();
        query.setDeptId(deptId);
        List<UserVO> list = userMapper.selectUser(query);
        if(list!=null && list.size()>0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        deptMapper.deleteById(deptId);
    }
}
